from selenium import webdriver
from selenium.webdriver.common.by import By


class BasePage(object):
    def __init__(self):
        try:
            self.driver = webdriver.Chrome()
        except Exception:
            raise NameError("Not Chrome")
        self.url = "http://cn.bing.com"

    # 打开页面
    def open(self, url):
        if url != "":
            self.driver.get(url)
            self.driver.maximize_window()
        else:
            raise ValueError("not value")

    # 定位元素
    def find_element(self, *location):
        return self.driver.find_element(*location)

    # 输入内容
    def input(self, location, value, clear_first=True, click_first=True):
        try:
            if click_first:
                self.find_element(*location).click()
            if clear_first:
                self.find_element(*location).clear()
            self.find_element(*location).send_keys(value)
        except AttributeError:
            print("%s 页面没有找到 %s 元素" %(self, location))

    def script(self, src):
        self.driver.execute_script(src)

    # 退出浏览器
    def quit(self):
        self.driver.quit()






